#! /usr/bin/python
# -*- coding: cp1250 -*-

import glob
import re
import string
import sys
import os
import pepe.p_txt.bigtxt as bt

class Select:
    """ """
    def __init__(self):
        """ """
        self.inputfile = ''
        self.outputfile = ''
        self.replace = ['','']
        self.skip_rows = 0
        self.skip_emptyrows = 1
        self.keep_header = 0
        self.keep_regex = ''
        self.drop_regex = ''
        self.cdelimiter = '^'
        self.cdelimiter_output = '^'
        self.keep_columns = ['']
        self.lino = 0
        self.emptyrowscnt = 0

    def outputRows(self):
        """ """
        self.emptyrowscnt = 0
        of = open(self.outputfile,'w')

        for infile in glob.glob(self.inputfile):
            self.lino = 0

            for line in bt.lines(infile):
                line = re.sub('\n', '', line)
                self.lino += 1
                write = 1

                #print '-',re.sub('\s{1,}','', line),'-'
                if self.skip_emptyrows == 1 and re.sub('\s{1,}','', line) == "": self.emptyrowscnt += 1
                else:
                    # keep if regex
                    if self.keep_regex <> '':
                        n = re.search(self.keep_regex, line)
                        if n: 
                            write = 1
                        else: write = 0
        
                    # drop if regex
                    if self.drop_regex <> '':
                        n = re.search(self.drop_regex, line)
                        if n: write = 0
        
                    # header
                    if self.keep_header == 1 and self.lino == 1: write = 1
        
                    # replace
                    if self.replace <> ['','']: line = re.sub(self.replace[0], self.replace[1], line)
                    
                    # skip rows 
                    if self.skip_rows >= self.lino: write = 0
                    
                    if write == 1: of.write(line+'\n')

        of.close()
        self.lino = 0
        if self.emptyrowscnt <> 0: print 'self.emptyrowscnt = :', str(self.emptyrowscnt)
        self.emptyrowscnt = 0

    def outputColumns(self):
        """ """
        self.emptyrowscnt = 0
        of = open(self.outputfile,'w')

        for infile in glob.glob(self.inputfile):
            self.lino = 0

            for line in bt.lines(infile):
                line = re.sub('\n', '', line)
                self.lino += 1
                write = 1

                if self.skip_emptyrows <> 0 and re.sub('\s{1,}','', line) == "": self.emptyrowscnt += 1
                else:
                    cols = string.split(line, self.cdelimiter)
                    #print cols
                    res = []
                    if self.keep_columns <> ['']:
                        for c in self.keep_columns:
                            res.append(cols[c])
        
                    # skip rows 
                    if self.skip_rows >= self.lino: write = 0
        
                    if write == 1: of.write(string.join(res, self.cdelimiter_output)+'\n')

        of.close()
        self.lino = 0
        if self.emptyrowscnt <> 0: print 'self.emptyrowscnt = :', str(self.emptyrowscnt)
        self.emptyrowscnt = 0

EXAMPLE = """

import pepe.p_utils.txt as ppt

t = ppt.Select()

## example of keep/drop rows

t.inputfile = 'a.dat'
t.outputfile = 'a01Rows.log'
t.keep_header = 1
t.skip_emptyrows = 1
#t.keep_regex = '^\d{1,}100'
t.drop_regex = '^\d{1,}100'
t.replace = ['\t','^']
t.outputRows()

## example of replace

t.inputfile = 'a01Rows.log'
t.outputfile = 'a02Rows.log'
t.replace = [',','']
t.outputRows()

## example of replace advanced

t.inputfile = 'a02Rows.log'
t.outputfile = 'a03Rows.log'
t.replace = ['\.(?P<A>\d{2})',',\g<A>']
t.outputRows()

## example of keep/drop columns

t.inputfile = 'a.dat'
t.outputfile = 'a01Columns.log'
t.cdelimiter = '\t'
t.keep_columns = [0,3]
t.outputColumns()

## example of joining multiple files

t.inputfile = '*.dat'
t.outputfile = 'a05Skipped5.log'
t.skip_rows = 5
t.outputRows()

"""

if __name__ == "__main__":
    """ """
    of = open('_txt.py','w')
    # write template file with example in current dir
    of.write(EXAMPLE)
    of.close()

